package com.smart.buss.service.impl;

import com.smart.buss.dao.BussUserMapper;
import com.smart.buss.service.BussTaskLogService;
import com.smart.buss.service.BussTaskService;
import com.smart.buss.service.BussUserService;
import com.smart.buss.vo.BussTask;
import com.smart.buss.vo.BussTaskLog;
import com.smart.buss.vo.BussUser;
import com.smart.buss.vo.resp.BussUserResp;
import com.smart.primary.common.util.ImgUtil;
import com.smart.primary.common.util.InviteCode;
import com.smart.primary.common.util.PropertiesUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.util.List;

@Service
public class BussUserServiceImpl implements BussUserService {

	@Resource
	private BussUserMapper bussUserMapper;
	@Autowired
	private BussTaskService bussTaskService;
	@Autowired
	private BussTaskLogService bussTaskLogService;

	@Override
	public BussUser queryObject(BussUser entity) throws Exception {
		return bussUserMapper.queryObject(entity);
	}

	@Override
	public List<BussUser> queryList(BussUser entity) throws Exception {
		return bussUserMapper.queryList(entity);
	}

	@Override
	public List<BussUser> queryListByPage(BussUser entity) throws Exception {
		return bussUserMapper.queryListByPage(entity);
	}

	@Override
	public Integer queryCount(BussUser entity) throws Exception {
		return bussUserMapper.queryCount(entity);
	}

	@Override
	public Integer insert(BussUser entity) throws Exception {
		return bussUserMapper.insert(entity);
	}

	@Override
	public Integer update(BussUser entity) throws Exception {
		return bussUserMapper.update(entity);
	}

	@Override
	public Integer delete(String id) throws Exception {
		return bussUserMapper.delete(id);
	}
	@Override
	public String getCode() throws Exception{
		String code=InviteCode.getCode(8);
		BussUser entity=new BussUser();
		entity.setCode(code);
		if(bussUserMapper.queryObject(entity)==null){
			return  code;
		}else {
			return getCode();
		}
	}

	@Override
	public String getCodeImg(String code, HttpServletRequest request) throws Exception {
		String resURL = PropertiesUtil.getString("res.upload.url");
		String filePath = resURL +"upload/task/image/"+code+".jpg";
		ImgUtil.getTwoCodeImage(PropertiesUtil.getString("res.code.url")+code,filePath);
		return "upload/task/image/"+code+".jpg";
	}

	@Override
	public BussUserResp info(BussUser entity) throws Exception {
		return bussUserMapper.info(entity);
	}

	@Override
	public List<BussUserResp> queryListInfo(String id) throws Exception {
		return bussUserMapper.queryListInfo(id);
	}

	public Boolean getDakeVip(String phone) throws Exception {
		Boolean bool=false;
		if(StringUtils.isBlank(phone)){
			return bool;
		}
		BussTask bussTask=new BussTask();
		bussTask.setIshide(1);
		bussTask=bussTaskService.queryObject(bussTask);
		if(bussTask!=null){
			BussTaskLog bussTasklog=new BussTaskLog();
			bussTasklog.setTaskId(bussTask.getId());
			bussTasklog.setUserId(phone);
			bussTasklog.setCheckStatus(1);
			try {
				bussTasklog=bussTaskLogService.queryObject(bussTasklog);
				if(bussTasklog==null){
					bool=true;
				}
			}catch (Exception e){}
		}
		return bool;
	}
}
